package cn.cyb.server.service;

import cn.cyb.server.common.exception.ServiceException;
import cn.cyb.server.domain.entity.SysUser;
import cn.cyb.server.domain.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Spring Security 实现类
 *
 * @author yuebo
 * @date 2024/12/25
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(UserDetailServiceImpl.class);

    @Autowired
    private ISysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("SysUserDetailServiceImpl username=" + username);
        SysUser sysUser = userService.selectUserByUserName(username);
        if (sysUser == null) {
            logger.info("登录用户：{} 不存在.", username);
            throw new ServiceException("user not fund");
        }
        return new LoginUser(sysUser);
    }
}
